package cn.com.homeyoda.aimate.aihealth.ui.kepu;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.KePuBannerList;
import cn.com.homeyoda.aimate.aihealth.bean.KePuBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTitleBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTypeListBean;
import cn.com.homeyoda.aimate.aihealth.bean.MultipleItem;
import cn.com.homeyoda.aimate.aihealth.bean.PopularListBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import cn.com.homeyoda.aimate.aihealth.ui.health_manage.HealthManageViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.message.MessageModel;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.http.ResponseThrowable;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class KePuViewModel extends BaseViewModel<Repository> {

    private List<MultipleItem<Object>> kePuList = new ArrayList<>();
    public StateLiveData<List<MultipleItem<Object>>> kePuData;
    public StateLiveData<MessageTypeListBean> kePuTypeData;

    public StateLiveData<KePuBannerList> bannerData;

    public StateLiveData<PopularListBean> popularListData;


    public KePuViewModel(@NonNull Application application, Repository model) {
        super(application, model);
        kePuTypeData = new StateLiveData<>();
        kePuData = new StateLiveData<>();
        kePuData.setValue(new ArrayList<>());
        bannerData = new StateLiveData<>();
        popularListData = new StateLiveData<>();
    }

    public void titleData() {


        model.getPopularType()
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(KePuViewModel.this)
                .subscribe(new Observer<MessageTypeListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        kePuTypeData.postLoading();
                    }

                    @Override
                    public void onNext(MessageTypeListBean messageTypeListBean) {
                        if(messageTypeListBean.getCode() == 200){
                            MessageTypeListBean.DataBean dataBean = new MessageTypeListBean.DataBean();
                            dataBean.setTypeName("全部");
                            dataBean.setTypeValue("0");
                            messageTypeListBean.getData().add(0,dataBean);
//                            stateLiveData.postSuccess();
//                            stateLiveData.setValue(messageTypeListBean);
                            kePuTypeData.postValueAndSuccess(messageTypeListBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        //关闭对话框
                        kePuTypeData.postError();
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        //请求刷新完成收回
                        kePuTypeData.postIdle();

                    }
                });

    }

    public void getKePuDataByType(int type) {
        List<MultipleItem<Object>> list = new ArrayList<>();
        list.addAll(kePuList);
        if (type != 0) Collections.shuffle(list);
        kePuData.getValue().clear();
        kePuData.postSuccess();
        kePuData.getValue().addAll(list);
        kePuData.postValueAndSuccess(kePuData.getValue());
    }

    public void getKePuData(String type,String pageNum,String pageSize) {


        model.getPopularList(type, pageNum, pageSize)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(KePuViewModel.this)
                .subscribe(new Observer<PopularListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
//                        stateIsShowData.postLoading();
                    }

                    @Override
                    public void onNext(PopularListBean popularListBean) {
                        if(popularListBean.getCode() == 200){
                            //UploadVideoBean data = uploadVideoBeanBaseResponse2.getData();
                            //stateLiveData.postSuccess();
                            //stateLiveData.getValue().add(data);
                            List<PopularListBean.RowsBean> rowsBeans = popularListBean.getRows();
                            kePuList.clear();
                            kePuList.add(new MultipleItem<>(MultipleItem.KP_BANNER, new PopularListBean.RowsBean()));
                            for(PopularListBean.RowsBean rowsBean: rowsBeans){
                                kePuList.add(new MultipleItem<>(MultipleItem.KP_NORMAL, rowsBean));
                            }
                            kePuData.postValueAndSuccess(kePuList);
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        Log.i("huahua","stateLiveData.getMessage()====="+e.getMessage());
                        Log.i("huahua","e.getMessage()====="+e.getMessage().toString());
                        //关闭对话框
                        kePuData.postError();
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        //请求刷新完成收回
                        kePuData.postIdle();

                    }
                });
    }


    public void getPopularLisData(String type,String pageNum,String pageSize) {

        model.getPopularList(type, pageNum, pageSize)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(KePuViewModel.this)
                .subscribe(new Observer<PopularListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
//                        stateIsShowData.postLoading();
                    }

                    @Override
                    public void onNext(PopularListBean popularListBean) {
                        if(popularListBean.getCode() == 200){
                            popularListData.postValueAndSuccess(popularListBean);
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        Log.i("huahua","stateLiveData.getMessage()====="+e.getMessage());
                        Log.i("huahua","e.getMessage()====="+e.getMessage().toString());
                        //关闭对话框
                        popularListData.postError();
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        //请求刷新完成收回
                        popularListData.postIdle();

                    }
                });
    }

    public void getBanner(){
        model.getKePuBannerList()
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(KePuViewModel.this)
                .subscribe(new Observer<KePuBannerList>() {
                    @Override
                    public void onSubscribe(Disposable d) {
//                        stateIsShowData.postLoading();
                    }

                    @Override
                    public void onNext(KePuBannerList kePuBannerList) {
                        if(kePuBannerList.getCode() == 200){

                            bannerData.postValueAndSuccess(kePuBannerList);
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        Log.i("huahua","stateLiveData.getMessage()====="+e.getMessage());
                        Log.i("huahua","e.getMessage()====="+e.getMessage().toString());
                        //关闭对话框
                        bannerData.postError();
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        //请求刷新完成收回
                        bannerData.postIdle();

                    }
                });
    }

}
